6.7 Graphic Windows (window)

1. Definition

The data type window provides an interface for the input and output of basic two-dimensional geometric objects (cf. section 5.1) using the X11 or SunView window system. There are two object code libraries libWx11.a, and libWxview.a containing implementations for both the basic X11 and the xview environments. Application programs using data type window have to be linked with one of these libraries (cf. section 1.6):

a) For the X11 window system: CC prog.c -lP -lG -lL -lWx11 -lX11 -lm

b) For the xview environment: CC prog.c -lP -lG -lL -lWx11 -lxview -lolgx -lX11 -lm

An instance W of the data type window is an iso-oriented rectangular window in the two-dimensional plane. The default representation of W on the screen is a 850 × 850 pixel square positioned in the upper right corner (cf. creation, variant c)). The coordinates and scaling of W used for drawing operations are defined by three double parameters: x0, the x-coordinate of the left side, x1, the x-coordinate of the right side, and y0, the y-coordinate of the bottom side. The y-coordinate of the top side of W is determined by the current size and shape of the window on the screen, which can be changed interactively. A graphic window supports operations for drawing points, lines, segments, arrows, circles, polygons, graphs, ...and for graphical input of all these objects using the mouse input device. Most of the drawing operations have an optional color argument. Possible colors are black (default), white, blue, green, red, violet, and orange. On monochrome displays all colors different from white are turned to black. There are 6 parameters used by the drawing operations: 1. The line width parameter (default value 1 pixel) defines the width of all kinds of lines (segments, arrows, edges, circles, polygons).

2. The line style parameter defines the style of lines. Possible line styles are solid (default), dashed, and dotted.

3. The node width parameter (default value 10 pixels) defines the diameter of nodes created by the draw_node and draw_filled_node operations.

4. The text mode parameter defines how text is inserted into the window. Possible values are transparent (default) and opaque.

5. The drawing mode parameter defines the logical operation that is used for setting pixels in all drawing operations. Possible values are src$\_mode$ (default) and xor$\_mode$. In src$\_mode$ pixels are set to the respective color value, in xor$\_mode$ the value is bitwise added to the current pixel value.

6. The redraw function parameter is used to redraw the entire window, whenever a redrawing is necessary, e.g., if the window shape on the screen has been changed. Its type is pointer to a void-function taking no arguments, i.e., void (*F)();


2. Creation

a) W (int xpix, int ypix, int xpos, int ypos)

b) W (int xpix, int ypix)

c) W

Variant a) creates a window W of physical size xpix×ypix pixels with its upper left corner at position (xpos, ypos) on the screen, variant b) places W into the upper right corner of the screen, and variant c) creates a 850×850 pixel window positioned into the upper right corner.

All three variants initialize the coordinates of W to x0 = 0, x1 = 100 and y0 = 0. The init operation (see below) can later be used to change the window coordinates and scaling.

&truecm &truecm & 3. Operations


3.1 Initialization


void init double x_0, double x_1, double y_0 sets the coordinates of W to x0,x1, and y0


void set_grid_mode int d Adds a rectangular grid with integer coordinates and grid distance d to W, if d > 0. Removes grid from W, if d≤ 0.


void init double x_0, double x_1, double y_0, int d like init( x0, x1, y0) followed by set_grid(d)


void clear W is erased.



3.2 Setting parameters


int set_line_width int pix Sets the line width parameter to pix pixels and returns its previous value.


line_style set_line_style linestyle s Sets the line style parameter to s and returns its previous value.


int set_node_width int pix Sets the node width parameter to pix pixels and returns its previous value.


text_mode set_text_mode text_mode m Sets the text mode parameter to m and returns its previous value.


drawing_mode set_mode drawing_mode m Sets the drawing mode parameter to m and returns its previous value.


void set_redraw void (*F)() Sets the redraw function parameter to F.


3.3 Reading parameters and window coordinates


int get_line_width returns the current line width.


line_style get_line_style returns the current line style.


int get_node_width returns the current node width.


text_mode get_text_mode returns the current text mode.


drawing_mode get_mode returns the current drawing mode.


double xmin returns x0, the minimal x-coordinate of W.


double ymin returns y0, the minimal y-coordinate of W.


double xmax returns x1, the maximal x-coordinate of W.


double ymax returns y1, the maximal y-coordinate of W.


double scale returns the number of pixels of a unit length line segment.



3.4 Drawing points


void draw_point double x, double y, color c=black draws the point (x, y) as a cross of a vertical and a horizontal segment intersecting at (x, y).


void draw_point point p, c=black draws point (p.xcoord(),p.ycoord()).


3.5 Drawing line segments


void draw_segment double x_1, double y_1, double x_2, double y_2, color c=black draws a line segment from (x1, y1) to (x2, y2).


void draw_segment point p, point q, color c=black draws a line segment from point p to point q.


void draw_segment segment s, color c=black draws line segment s.


3.6 Drawing lines


void draw_line double x_1, double y_1, double x_2, double y_2, color c=black draws a straight line passing through points (x1, y1) and (x2, y2).


void draw_line point p, point q, color c=black draws a straight line passing through points p and q.


void draw_line line l, color c=black draws line l.


void draw_hline double y, color c=black draws a horizontal line with y-coordinate y.


void draw_vline double x, color c=black draws a vertical line with x-coordinate x.


3.7 Drawing arrows


void draw_arrow double x_1, double y_1, double x_2, double y_2, color c=black draws an arrow pointing from (x1, y1) to (x2, y2).

void draw_arrow point p, point q, color c=black draws an arrow pointing from point p to point q.


void draw_arrow segment s, color c=black draws an arrow pointing from s.start() to s.end().


3.8 Drawing circles


void draw_circle double x, double y, double r, color c = black draws the circle with center (x, y) and radius r.


void draw_circle point p, double r, color c = black draws the circle with center p and radius r.


void draw_circle circle C, color c = black draws circle C.


3.9 Drawing discs


void draw_disc double x, double y, double r, color c = black draws a filled circle with center (x, y) and radius r.


void draw_disc point p, double r, color c = black draws a filled circle with center p and radius r.


void draw_disc circle C, color c = black draws filled circle C.



3.10 Drawing polygons


void draw_polygon listpoint lp, color c=black draws the polygon with vertex sequence lp.


void draw_polygon polygon P, color c=black draws polygon P.


void draw_filled_polygon listpoint lp, color c=black draws the filled polygon with vertex sequence lp.


void draw_filled_polygon polygon P, color c=black draws filled polygon P.



3.11 Drawing functions


void plot_xy double x_0, double x_1, (double)(*F)(double), color c = black draws function F in range [x0, x1], i.e., all points (x, y) with y = F(x) and x0xx1


void plot_yx double y_0, double y_1, (double)(*F)(double), color c = black draws function F in range [y0, y1], i.e., all points (x, y) with x = F(y) and y0yy1


3.12 Drawing text


void draw_text double x, double y, string s, color c = black writes string s starting at position (x, y).


void draw_text point p, string s, color c = black writes string s starting at position p.


void draw_ctext double x, double y, string s, color c = black writes string s centered at position (x, y).


void draw_ctext point p, string s, color c = black writes string s centered at position p.


3.13 Drawing nodes


void draw_node double x_0, double y_0, color c = black draws a node at position (x0, y0).


void draw_node point p, color c = black draws a node at position p.


void draw_filled_node double x_0, double y_0, color c = black draws a filled node at position (x0, y0).


void draw_filled_node point p, color c = black draws a filled node at position p.


void draw_text_node double x, double y, string s, color c = black draws a node with label s at position (x0, y0).


void draw_text_node point p, string s, color c = black draws a node with label s at position p.


void draw_int_node double x, double y, int i, color c = black draws a node with integer label i at position (x0, y0).


void draw_int_node point p, int i, color c = black draws a node with integer label i at position p.


3.14 Drawing edges


void draw_edge double x_1, double y_1, double x_2, double y_2, color c = black draws an edge from (x1, y1) to (x2, y2).


void draw_edge point p, point q, color c = black draws an edge from p to q.


void draw_edge segment s, color c = black draws an edge from s.start() to s.end().


void draw_edge_arrow double x_1, double y_1, double x_2, double y_2, color c = black draws a directed edge from (x1, y1) to (x2, y2).


void draw_edge_arrow point p, point q, color c = black draws a directed edge from p to q.


void draw_edge_arrow segment s, color c = black draws a directed edge from s.start() to s.end().


3.15 Mouse Input


int read_mouse displays the mouse cursor until a button is pressed. Returns integer 1 for the left, 2 for the middle, and 3 for the right button (-1,-2,-3, if the shift key is pressed simultaneously).


int read_mouse double& x, double& y displays the mouse cursor on the screen until a button is pressed. When a button is pressed the current position of the cursor is assigned to to (x, y) and the pressed button is returned.


int read_mouse_seg double x_0, double y_0, double& x, double& y displays a line segment from (x0, y0) to the current cursor position until a mouse button is pressed. When a button is pressed the current position is assigned to (x, y) and the pressed button is returned.


int read_mouse_rect double x_0, double y_0, double& x, double& y displays a rectangle with diagonal from (x0, y0) to the current cursor position until a mouse button is pressed. When a button is pressed the current position is assigned to (x, y) and the pressed button is returned.


int read_mouse_circle double x_0, double y_0, double& x, double& y displays a circle with center (x0, y0) passing through the current cursor position until a mouse button is pressed. When a button is pressed the current position is assigned to (x, y) and the pressed button is returned.


bool confirm string s displays string s and asks for confirmation. Returns true iff the answer was ``yes''.


void acknowledge string s displays string s and asks for acknowledgement.


int read_panel string h, int n, string* S displays a panel with header h and an array S[1..n] of n string buttons, returns the index of the selected button.


int read_vpanel string h, int n, string* S like read_panel with vertical button layout


int read_int string p displays a panel with prompt p for integer input, returns the input


double read_real string p displays a panel with prompt p for real input returns the input


string read_string string p displays a panel with prompt p for string input, returns the input


void message string s displays message s (each call adds a new line).


void del_message deletes the text written by all previous message operations.



3.16 Input and output operators

For input and output of basic geometric objects in the plane such as points, lines, line segments, circles, and polygons the < < and > > operators can be used. Similar to input streams windows have an internal state indicating whether there is more input to read or not. Its initial value is true and it is turned to false if an input sequence is terminated by clicking the right mouse button (similar to ending stream input by the eof character). In conditional statements objects of type window are automatically converted to boolean by returning this internal state. Thus, they can be used in conditional statements in the same way as input streams. For example, to read a sequence of points terminated by a right button click, use `` while (W  > >  p) { ... } ''.

3.16.1 Output

&truecm &truecm & window& « point p like W.draw_point(p)


window& « segment s like W.draw_segment(s)


window& « line l like W.draw_line(l)


window& « circle C like W.draw_circle(C)


window& « polygon P like W.draw_polygon(P)



3.16.2 Input


window& » p reads a point p: clicking the left button assigns the current cursor position to p.


window& » s reads a segment s: use the left button to input the start and end point of s.


window& » l reads a line l: use the left button to input two different points on l


window& » C reads a circle C: use the left button to input the center of C and a point on C


window& » P reads a polygon P: use the left button to input the sequence of vertices of P, end the sequence by clicking the middle button.


As long as an input operation has not been completed the last read point can be erased by simultaneously pressing the shift key and the left mouse button.